package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linzp.bishe.common.Result;
import com.linzp.bishe.common.SaveAddress;
import com.linzp.bishe.entity.DisciplineCompetition;
import com.linzp.bishe.mapper.DisciplineCompetitionMapper;
import com.linzp.bishe.model.DisciplineCompetitionModel;
import com.linzp.bishe.service.DisciplineCompetitionService;
import com.linzp.bishe.service.ParticipantsService;
import com.linzp.bishe.utils.DealFile;
import com.linzp.bishe.utils.TableName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@Service
public class DisciplineCompetitionServiceImpl extends ServiceImpl<DisciplineCompetitionMapper, DisciplineCompetition> implements DisciplineCompetitionService {
	@Autowired
	private SaveAddress saveAddress;

	@Autowired
	private DisciplineCompetitionMapper disciplineCompetitionMapper;

	@Autowired
	private ParticipantsService participantsService;
	@Override
	public String saveMultipartFile(MultipartFile file) {
		return DealFile.saveMultipartFile(file,saveAddress.getAllAddress()+saveAddress.getCache());
	}

	@Override
	public void saveCacheToFile(String name) {
		DealFile.saveCacheToFile(saveAddress.getAllAddress()+saveAddress.getCache(),
				saveAddress.getAllAddress()+saveAddress.getDisciplineCompetition(),name);
		DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getCache()+"/"+name);
	}

	@Override
	public void getByIdAllDisciplineCompetition(Result success, Integer id, Integer current,
	                                            Integer page, String searchCompetitionName, String searchStatus) {
		if (searchCompetitionName!=null & !searchCompetitionName.isEmpty()){
			searchCompetitionName = '%'+searchCompetitionName+'%';
		}
		success.put("tableData",disciplineCompetitionMapper.getByIdAllDisciplineCompetition(id, TableName.DISCIPLINE,
				searchCompetitionName, searchStatus,(current-1) * page,page));
		success.put("total",disciplineCompetitionMapper.getByIdAllDisciplineCompetitionCount(id, TableName.DISCIPLINE,
				searchCompetitionName, searchStatus));
		success.put("loadAddress",saveAddress.getAccessPath()+saveAddress.getDisciplineCompetition());
	}

	@Override
	public String updateMultipartFile(MultipartFile file, String fileName) {
		String name = DealFile.upDateMultipartFile(file, saveAddress.getAllAddress() + saveAddress.getDisciplineCompetition(), fileName);
		if (!name.equals(fileName)){
			DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getDisciplineCompetition()+"/"+fileName);
		}
		return name;
	}

	@Override
	public void deleteDisciplineCompetition(DisciplineCompetitionModel disciplineCompetitionModel) {
		DisciplineCompetition competition = disciplineCompetitionModel.getDisciplineCompetition();
		this.removeById(competition.getId());
		String materials = competition.getCertificate();
		if (materials !=null){
			if(!materials.isEmpty()){
				DealFile.DeleteFolder(saveAddress.getAllAddress()+saveAddress.getDisciplineCompetition()+"/"+materials);
			}
		}
		participantsService.removeListById(disciplineCompetitionModel.getParticipants());
	}

	/**
	 * @Description 处理状态
	 * @Param [id, status]
	 * @return void
	 */
	@Override
	public void dealStatus(Integer id, String status) {
		UpdateWrapper<DisciplineCompetition> wrapper = new UpdateWrapper<>();
		wrapper.eq("id",id);
		wrapper.set("status",status);
		this.update(wrapper);
	}

	@Override
	public boolean cheakRepeat(String element1) {
		QueryWrapper<DisciplineCompetition> wrapper = new QueryWrapper<>();
		wrapper.eq("work_name",element1);
		return this.getOne(wrapper,false)!=null ? true:false;
	}
}
